package br.mil.eb.pqrmnt7.scc.controller.jsf.controleabastecimento;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.inject.Named;

import br.mil.eb.pqrmnt7.scc.controller.jsf.AppMB;
import br.mil.eb.pqrmnt7.scc.entity.TipoCombustivel;
import br.mil.eb.pqrmnt7.scc.entity.controleabastecimento.CalculoSaldoCombustivelException;
import br.mil.eb.pqrmnt7.scc.entity.controleabastecimento.LinhaControleSaldoCombustivel;
import br.mil.eb.pqrmnt7.scc.facade.ISaldoCombustivel;

import com.powerlogic.jcompany.commons.annotation.PlcUriIoC;
import com.powerlogic.jcompany.commons.config.stereotypes.SPlcMB;
import com.powerlogic.jcompany.config.aggregation.PlcConfigAggregation;
import com.powerlogic.jcompany.config.collaboration.FormPattern;
import com.powerlogic.jcompany.config.collaboration.PlcConfigForm;
import com.powerlogic.jcompany.config.collaboration.PlcConfigFormLayout;
import com.powerlogic.jcompany.controller.jsf.PlcEntityList;
import com.powerlogic.jcompany.controller.jsf.annotations.PlcHandleException;
import com.powerlogic.jcompany.domain.validation.PlcMessage;

//Essa é a entidade que será disponibilizada nos formulários e campos xhtml
@PlcConfigAggregation(
	entity = br.mil.eb.pqrmnt7.scc.entity.controleabastecimento.LinhaControleSaldoCombustivelEntity.class
)
@PlcConfigForm (
	formPattern=FormPattern.Ctl,
	formLayout = @PlcConfigFormLayout(dirBase="/WEB-INF/fcls/controleabastecimento")
)
@SPlcMB
@PlcUriIoC("controleabastecimento")
@Named("controleAbastecimentoMB")
@PlcHandleException
public class ControleAbastecimentoMB extends AppMB  {

	private static final long serialVersionUID = 1L;
	
	@Inject
	private ISaldoCombustivel saldoCombustivelService;
	
	private Date anoMesReferencia;
	
	private Date dataInicio;
	
	private Date dataFim;
	
	private TipoCombustivel tipoCombustivel;
	
	@Produces  
	//@Named("controlesaldocombustivelLista") Deu erro!
	@Named("linhasaldocombustivelLista")
	public PlcEntityList createEntityListPlc() {
		if (this.entityListPlc==null) {
			this.entityListPlc = new PlcEntityList();
			this.newObjectList();
		}
		return this.entityListPlc;
	}	
	
	public Date getAnoMesReferencia() {
		return anoMesReferencia;
	}

	public void setAnoMesReferencia(Date anoMesReferencia) {
		this.anoMesReferencia = anoMesReferencia;
	}
	
	public Date getDataInicio() {
		return dataInicio;
	}

	public void setDataInicio(Date dataInicio) {
		this.dataInicio = dataInicio;
	}

	public Date getDataFim() {
		return dataFim;
	}

	public void setDataFim(Date dataFim) {
		this.dataFim = dataFim;
	}
	
	public TipoCombustivel getTipoCombustivel() {
		return tipoCombustivel;
	}

	public void setTipoCombustivel(TipoCombustivel tipoCombustivel) {
		this.tipoCombustivel = tipoCombustivel;
	}
	
	public String calculaSaldosCombustiveis() {
		
		log.info("Controle de Abastecimento solicitado interativamente para o período de " +
				(dataInicio) +" a " + (dataFim));
		
		log.info("TipoCombustivel: " + this.tipoCombustivel);
		
		try {
			Long saldoDiesel = 0L;
			Long saldoGasolina = 0L;
			Long saldoCombustivel = 0L;
			List<LinhaControleSaldoCombustivel> linhas;
			if (this.tipoCombustivel != null) {
				log.info("saldoCombustivelService.recuperaRegistrosDeControle: " + tipoCombustivel);
				saldoCombustivel = saldoCombustivelService.calculaSaldo(this.tipoCombustivel, dataInicio, dataFim);
				linhas = saldoCombustivelService.recuperaRegistrosDeControle(this.tipoCombustivel, dataInicio, dataFim);
				
				if (tipoCombustivel.isDiesel()) {
					msgUtil.msg("{calculosaldodiesel.total.ok}",
							new String[] { saldoCombustivel + "" }, PlcMessage.Cor.msgAzulPlc.toString());	
				} else if (tipoCombustivel.isGasolina()) {
					msgUtil.msg("{calculosaldogasolina.total.ok}",
							new String[] { saldoCombustivel + "" }, PlcMessage.Cor.msgAzulPlc.toString());	
				} else {
					log.error("Não é um tipo de combustível válido: " + tipoCombustivel);
					msgUtil.msgError("{calculosaldo.erros}", new String[] { "" });
				}
				
				
			} else {
				saldoDiesel = saldoCombustivelService.calculaSaldo(TipoCombustivel.DIESEL, dataInicio, dataFim);
				saldoGasolina = saldoCombustivelService.calculaSaldo(TipoCombustivel.GASOLINA, dataInicio, dataFim);
				linhas = saldoCombustivelService.recuperaRegistrosDeControle(dataInicio, dataFim);
				
				msgUtil.msg("{calculosaldo.total.ok}",
						new String[] { saldoDiesel + "", saldoGasolina + "" }, PlcMessage.Cor.msgAzulPlc.toString());				
				
			}
			
			List<Object> linhasO = new ArrayList<Object>(linhas.size());
			
			log.debug("linhas.size(): " + linhas.size());
			
			linhasO.addAll(linhas);
			
			createEntityListPlc().setItensPlc(linhasO);
			
		} catch (CalculoSaldoCombustivelException e) {
			e.printStackTrace();
			log.error(e);
			msgUtil.msgError("{calculosaldo.erros}", new String[] { "" });
		}
		
		return null;
	}

}